package com.ruoyi.utils;

import c.d.e.DetectModelGrpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import java.util.Arrays;
import java.util.List;

public class RpcRequest {
    DetectModelGrpc.DetectModelBlockingStub stub;
    ManagedChannel channel;

    public String ip;

    public int port;

    public String bucket_name;

    // 初始化类，需要入参算法的ip和port
    public RpcRequest(String ip, int port, String bucket_name){
        this.ip = ip;
        this.port = port;
        this.bucket_name = bucket_name;
        channel = ManagedChannelBuilder
                .forAddress(ip, port)
                .usePlaintext()
                .build();
        stub = c.d.e.DetectModelGrpc.newBlockingStub(channel);
    }

    // 将检测结果封装为之前使用http返回的response.Body中的检测结果格式
    private String getRetString(String img_name, List<String> res_name_lst, Double[][] con_lst, List<Double> acc_lst){
        StringBuilder builder = new StringBuilder();
        builder.append("[{\"imgName\":\"").append(img_name).append("\",");
        builder.append("\"tag\":").append(ListToString(res_name_lst)).append(",");
        builder.append("\"corner\":").append(Arrays.deepToString(con_lst)).append(",");
        builder.append("\"accuracy\":").append(acc_lst);
        builder.append("}]");
        return builder.toString();
    }

    // 专门为返回检测虫名列表转换为string的函数
    private <T> String ListToString(List<T> lst){
        if(lst.size()==0){
            return "[]";
        }
        StringBuilder builder = new StringBuilder();
        builder.append(lst.toString().replace(",","\",\"").replace("[","[\"").replace("]","\"]").replace(" ",""));
        return builder.toString();
    }

    // 调用算法并返回结果
    public String getRpcRes(String req_oss_url){
        c.d.e.DetectedResultWithOss reply = stub.detectWithOss(c.d.e.ImageWithOss
                .newBuilder().setImgPath(req_oss_url).setBucketName(this.bucket_name).build());
        String ori_path = reply.getImgName();
        List<String> res_name_lst = reply.getTagList();
        Double[][] con_lst;
        if(!reply.getCornerList().isEmpty()){
            con_lst = new Double[reply.getCornerList().size()][4];
            int i = 0;
            for(int k=0;k<reply.getCornerList().size();k++){
                con_lst[i][0] = reply.getCornerList().get(k).getX1();
                con_lst[i][1] = reply.getCornerList().get(k).getY1();
                con_lst[i][2] = reply.getCornerList().get(k).getX2();
                con_lst[i][3] = reply.getCornerList().get(k).getY2();
                i++;
            }
        }else{
            con_lst = new Double[0][0];
        }
        if(con_lst.length==0){
            return "[]";
        }
        List<Double> res_acc_lst = reply.getAccuracyList();
        String res_path = reply.getResultPath();
        String[] name_lst = req_oss_url.split("/");
        String name = name_lst[name_lst.length-1];
        return getRetString(name, res_name_lst, con_lst, res_acc_lst);
    }

    public void shutdownChannel(){
        this.channel.shutdown();
    }
}
